package com.xjrsoft.module.test;

import com.xjrsoft.module.magicapi.service.IMagicApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;
import tech.powerjob.worker.log.OmsLogger;

/**
 * @Author: tzx
 * @Date: 2023/6/20 17:04
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class DemoPowerJobProcessor implements BasicProcessor {

    private final IMagicApiService magicApiService;

    @Override
    public ProcessResult process(TaskContext context) throws Exception {
        // 在线日志功能，可以直接在控制台查看任务日志，非常便捷
        OmsLogger omsLogger = context.getOmsLogger();
        omsLogger.info("BasicProcessorDemo start to process, current JobParams is {}.", context.getJobParams());


        log.info("BasicProcessorDemo start to process, current JobParams is {}.", context.getJobParams());
        // TaskContext为任务的上下文信息，包含了在控制台录入的任务元数据，常用字段为
        // jobParams（任务参数，在控制台录入），instanceParams（任务实例参数，通过 OpencesAPI 触发的任务实例才可能存在该参数）

        if (context.getJobParams().contains("xxxx")) {
            // 调用某个接口
            magicApiService.executeApi("xxxxxx");
        }
        else{
            // 调用某个接口
            magicApiService.executeApi("xxxxxx");
        }

        // 进行实际处理... com.xjrsoft.module.test.DemoPowerJobProcessor

        // 返回结果，该结果会被持久化到数据库，在前端页面直接查看，极为方便
        return new ProcessResult(true, "result is xxx");
    }
}
